package sampleapps;


import umacis.implementation.ActionHook;
import umacis.implementation.DocumentHook;

import com.sun.lwuit.Image;



/**
 * Abstracted from the user interface...
 */
public class Calculator { // extends Actions {

	private static final int MAX_NUMBERSIZE = 8;
	private static final int PLUS = 0;
	private static final int MINUS = 1;
	private static final int NULL = 2;
	
	int arg1, arg2;
	private int argCount;
	private int currentOperation;
	private DocumentHook numberDisplay = new DocumentHook("numberVisibleToUser", 0);;
	private boolean arg2IsBeingFilled;

	public Calculator()  {
		System.out.println("Hello");
		
		
		//Image t = new Image(t); //ListHook, ChoiceHook, LabelHook, TextHook, TreeHook, CalendarHook, SwitchHook, ProgressHook, ImageHook
		
		new ActionHook("Subtract") {  public void  x() { subtract(); }}; 
		//ImplementationHook ExecutionHook MethodHook
		new ActionHook("Add") { public void x() { add(); }};
//		new ActionHook("Calculate Result") { public void execute() { calculateResult(); }};
//		new ActionHook("actOnNumberBeingPunched") { public void execute() { actOnNumberBeingPunched(); }};
//		new ActionHook("startOver") { public void execute() { startOver(); }};
//		new ActionHook("zeroKey") { public void execute() { zeroKey(); }};
	}
	
private void add() {
	}

private void subtract() {
	}

//	public void declareActionHooks() {
//		System.out.println("from declareHooks");
//		
//		new ActionHook("Subtract") { public void execute() { subtract(); }};
//		new ActionHook("Add") { public void execute() { add(); }};
//		new ActionHook("Calculate Result") { public void execute() { calculateResult(); }};
//		new ActionHook("actOnNumberBeingPunched") { public void execute() { actOnNumberBeingPunched(); }};
//		new ActionHook("startOver") { public void execute() { startOver(); }};
//		new ActionHook("zeroKey") { public void execute() { zeroKey(); }};
//	}

	//actions****************************************************************
	
	
	public String action00_Name() {	return "Add"; }
	//alternative method again:
	public String action00(boolean askingForName) {
		if (askingForName) return "Action00";
		System.out.println("Hello from calculator!!add");
		currentOperation = PLUS;
		processArguments();
		return null;
	}
	
	public String action01_Name() {	return "Subtract"; }
	public void action01_Body() { 
		currentOperation = MINUS;
		processArguments();
		System.out.println("Hello from calculator!!subtract");
	}
	
	public String action02_Name() {	return "Startover"; }
	public void action02_Body() {
		System.out.println("Hello from calculator!!startOver");
		//HookManager ui = null;
		numberDisplay.set(arg1 = 0);
		currentOperation = NULL;
		
		arg2IsBeingFilled = false;
	}
	
	public String action03_Name() {	return "Calculate Result"; }
	public void action03_Body() {
		System.out.println("Hello from calculator!!calculateResult");
		if (currentOperation != NULL && !arg2IsBeingFilled) {  arg2IsBeingFilled=true; }
		processArguments();
	}
	
	public String action04_Name() {	return "ActOnKeyRelease"; }
	public void action04_Body() {
		System.out.println("Hello from calculator!!actOnNumberBeingPunched");
		if (currentOperation != NULL && !arg2IsBeingFilled)	{
			
			arg2IsBeingFilled = true;
			numberDisplay.clear(); //clear input/result box
		}
	}
	
	public String action05_Name() {	return "zeroKey"; }
	public void action05_Body() {
		System.out.println("Hello from calculator!!zeroKey");
	}
	
	//private methods *************************************************************************
	private void processArguments() {
		if ( arg2IsBeingFilled ) {
			arg2 = numberDisplay.get();
			numberDisplay.set(operate(currentOperation, arg1, arg2));
			arg2IsBeingFilled = false;
		}
		else  { 
			arg1 = numberDisplay.get();
		}
	}
	
	private int operate(int operation, int v1, int v2) {
		switch (operation) {
		case PLUS : return v1 + v2;
		case MINUS : return v1 - v2;
		
		default : ;
		
		}
		return 0;
	}	
	
}
